
// 遍历函数
import { toMatrix } from '../math/array.js'

// 连续
// Continuous signal
// serial
export const _traversal = ({ ctx, points, n = 2, loop, iter, init, discrete = false }) => {
    let len = points.length
    let result = []
    let _iter, _init
    if (ctx) {
        _iter = (ps, i) => {
            iter(ctx, ps, i)
        }
        _init = (ps) => {
            if (init) {
                init(ctx, ps ? ps[0] : points[0])
            }
        }
    } else {
        _iter = (ps, i) => {
            result[result.length] = iter(ps, i)
        }
        _init = (ps) => {
            if (init) {
                result[result.length] = init(ps ? ps[0] : points[0])
            }

        }
    }



    if (discrete) {
        // 离散
        let matrix = toMatrix(points, n)
        matrix.forEach((ps, i) => {
            if (ps.length === n) {
                _init(ps)
                _iter(ps, i)
            }
        })

    } else {
        if (init) { _init() }
        // 连续
        if (loop) {
            for (let i = 0; i < len; i += n - 1) {
                let ps = []
                for (let j = 0; j < n; j++) {
                    ps.push(points[(i + j) % len])
                }
                _iter(ps, i)
            }
        } else {
            for (let i = 0; i < len - n + 1; i += n - 1) {
                let ps = []
                for (let j = 0; j < n; j++) {
                    ps.push(points[i + j])
                }
                _iter(ps, i)
            }
        }
    }

    return result
}



// 离散
// discrete signal
// const _discrete = ({ points, n, iter }) => {
//     let matrix = toMatrix(points, n)
//     return matrix.map(ps => {
//         if (ps.length === n) {
//             return iter(ps)
//         }
//         return ''
//     })
// }

// export const traversalThreePoints = ({ points, loop, iter, init }) => {
//     let n = 3
//     return _traversal({ points, n, loop, iter, init })
// }


// export const traversalFourPoints = ({ points, loop, iter, init }) => {
//     let n = 4
//     return _traversal({ points, n, loop, iter, init })
// }

// export const traversalFivePoints = ({ points, loop, iter, init }) => {
//     let n = 5
//     return _traversal({ points, n, loop, iter, init })
// }
// export const discreteFourPoints = ({ points, iter }) => {
//     let n = 4
//     return _discrete({ points, n, iter })
// }

// export const discreteFivePoints = ({ points, iter }) => {
//     let n = 5
//     return _discrete({ points, n, iter })
// }